package com.cskaoyan.service;


import com.cskaoyan.bean.*;
import com.cskaoyan.bo.BrandBO;
import com.cskaoyan.mapper.BrandMapper;
import com.cskaoyan.mapper.StorageMapper;
import com.cskaoyan.vo.BrandVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Date;
import java.util.List;

@Service
public class BrandServiceImpl implements BrandService{

    @Autowired
    StorageMapper storageMapper;
    @Autowired
    BrandMapper brandMapper;
    @Override
    public BrandVO queryBrand(Integer page, Integer limit, Integer id, String name, String sort, String order) {

        PageHelper.startPage(page, limit); //mybatis在构建sql语句 👉 会构造limit和offset

        BrandExample brandExample = new BrandExample();

        brandExample.setOrderByClause(sort + " " + order);


        BrandExample.Criteria criteria = brandExample.createCriteria();

        if (id != null && !"".equals(id)) {
            criteria.andIdEqualTo(id);
        }
        if (name != null && !"".equals(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        List<Brand> brands = brandMapper.selectByExample(brandExample);

        PageInfo<Brand> brandPageInfo = new PageInfo<>(brands);//查询结果放进来
        long total = brandPageInfo.getTotal();
        BrandVO brandVO = new BrandVO();
        brandVO.setTotal(total);
        brandVO.setItems(brands);
        return brandVO;
    }

    @Override
    public void brandDelete(BrandBO brandBO) {
        BrandExample brandExample = new BrandExample();
        int key = brandMapper.deleteByPrimaryKey(brandBO.getId());
    }
    @Override
    @SneakyThrows
    public Storage storageUpload(MultipartFile file) {

        File path = new File("D:\\code");

        Storage storage = new Storage();

        storage.setAddTime(new Date());

        storage.setDeleted(false);

        storage.setUpdateTime(new Date());

        storage.setSize((int) file.getSize());

        storage.setKey(file.getOriginalFilename());

        storage.setName(file.getOriginalFilename());

        storage.setType(file.getContentType());

        storage.setUrl("http://localhost:8083/"+file.getOriginalFilename());

        File files = new File(path, file.getOriginalFilename());

        file.transferTo(files);

        int i = storageMapper.insertSelective(storage);

        StorageExample storageExample = new StorageExample();

        StorageExample.Criteria criteria = storageExample.createCriteria();

        criteria.andDeletedEqualTo(false);

        Storage storages = storageMapper.selectByPrimaryKey(storage.getId());

        return storages;

    }

    @Override
    public Brand brandCreate(Brand brand) {

        Brand brands = new Brand();
        brand.setAddTime(new Date());
        brand.setUpdateTime(new Date());
        brand.setDeleted(false);
        BeanUtils.copyProperties(brand, brands);

        brandMapper.insert(brand);

        Brand brand1 = brandMapper.selectByPrimaryKey(brand.getId());

        return brand1;

    }

    @Override
    public Brand brandUpdate(Brand brand) {
        Brand brands = new Brand();
        brands.setName(brand.getName());
        brands.setDesc(brand.getDesc());
        brands.setPicUrl(brand.getPicUrl());
        brands.setFloorPrice(brand.getFloorPrice());
        brand.setUpdateTime(new Date());
        brandMapper.updateByPrimaryKeySelective(brands);
        return brandMapper.selectByPrimaryKey(brands.getId());
    }

}
